# Extension for trend in outcomes.

# libs --------------------------------------------------------------------
library(here)
library(tidyverse)
library(haven)

# Read in cleaned dataset
data <- read_csv(file = '../data/analysis/analysis.csv')


#### Outcome Plots by state
# DVS
data <- data %>% rowwise() %>% 
  mutate(dem_share_mean = mean(c(dem_share_gov, dem_share_pres, dem_share_sen), na.rm=T))

data %>%  group_by(state, year) %>% 
  summarize(dvs = mean(dem_share_mean, na.rm=T)) %>% 
  filter(!is.na(dvs)) %>% # remove state years with no statewide elections (ie UT in 2002) %>% 
  filter(state %in% c("WA", "UT", "MT", "CA")) %>%
  ggplot(aes(x = year, y = dvs, color = state)) + 
  geom_point() + 
  geom_path() +
  theme_bw() #+
  #labs(title = 'State Level Democratic Two Party Vote Share', x = 'Year', y = 'Dem Vote Share, Mean Statewide Office') +
  ggsave(filename = '../newfig/StateDVS_Mean.png', width = 5, height = 4, units = 'in')

# Pres DVS
data %>% group_by(state, year) %>%  
  filter(!is.na(ballots_cast)) %>% 
  summarize(dem_share_pres = weighted.mean(dem_share_pres, ballots_cast)) %>% 
  filter(year %% 4 == 0) %>% 
  filter(state %in% c("CA", "UT", "MT", "WA")) %>%
  ggplot(aes(x = year, y = dem_share_pres, color = state)) + 
  geom_point() + 
  geom_path() +
  theme_bw() + 
  scale_x_continuous(breaks = seq(1996,2020,4)) +
  labs(x = 'Year', y = 'Dem Vote Share, Pres') +
  theme(legend.position = c(.25, .9), legend.title = element_blank(), legend.direction = 'horizontal') +
  ggsave(filename = '../newfig/StateDVS_Pres.png')

# Turnout
data %>%
  group_by(state, year) %>%
  filter(!is.na(ballots_cast)) %>% 
  summarize(turnout_share = weighted.mean(turnout_share, ballots_cast)) %>% 
  mutate(type = ifelse(year %% 4 == 0, 'Presidential', 'Midterm')) %>% 
  filter(state %in% c("CA", "UT", "MT", "WA")) %>%
  ggplot(aes(x = year, y = turnout_share, color = state)) +
  geom_point() +
  geom_path() +
  theme_bw() +
  facet_wrap(~type) +
  labs(x = 'Year', y = 'Turnout') + 
  scale_x_continuous(breaks = seq(1996,2020,4)) +
  theme(legend.position = c(.25, .9), legend.title = element_blank(), legend.direction = 'horizontal') +
  ggsave(filename = '../newfig/StateTurnout.png', width = 6, height = 4, units = 'in')


  # Pres Turnout
data %>%  group_by(state, year) %>%
  summarize(turnout_share = mean(turnout_share,na.rm=T)) %>% 
  filter(year %% 4 == 0) %>% 
  filter(state %in% c("NV", "UT", "MT", "VT")) %>%
  ggplot(aes(x = year, y = turnout_share, color = state)) +
  geom_point() +
  geom_path() +
  theme_bw() + 
  scale_x_continuous(breaks = seq(1996,2020,4)) +
  labs(title = 'Turnout in Presidential Election Years over time,\n by State', y = 'Turnout Share', x = 'Year') +
    ggsave(filename = '../newfig/StateTurnout_Pres.png', width = 5, height = 4, units = 'in')

# By Treatment ----
data %>% group_by(state, year, treat) %>%  
  summarize(dvs = mean(dem_share_mean, na.rm=T)) %>% # ignore races that don't exist in that state-year
  filter(state %in% c("CA", "UT", "MT", "WA"), !is.na(dvs)) %>% #ignore state-years with no elections
  ggplot(aes(x = year, y = dvs, color = as.logical(treat))) + 
  geom_point() + 
  geom_path() +
  theme_bw() + 
  facet_wrap(~state) +
  scale_x_continuous(breaks = seq(1996,2020,4)) +
  labs(x = 'Year', y = 'Dem Vote Share, All Races', color = 'Treated') +
  theme(legend.direction = 'horizontal', legend.position = c(.2, .39)) +
  ggsave(filename = '../newfig/DVS_Treatment.png')

# Pres Turnout
data %>%
  group_by(state, year, treat) %>%
  filter(!is.na(ballots_cast)) %>% 
  summarize(turnout_share = weighted.mean(turnout_share, ballots_cast)) %>% 
  mutate(type = ifelse(year %% 4 == 0, 'Presidential', 'Midterm')) %>% 
  filter(state %in% c("CA", "UT", "MT", "WA")) %>%
  ggplot(aes(x = year, y = turnout_share, color = as.logical(treat))) +
  geom_point() +
  geom_path() +
  theme_bw() +
  facet_wrap(~type) +
  labs(x = 'Year', y = 'Turnout', color = 'Treated') + 
  scale_x_continuous(breaks = seq(1996,2020,4)) + 
  facet_wrap(~state) +
  theme(legend.direction = 'horizontal', legend.position = c(.2, .39)) +
    ggsave(filename = '../newfig/Turnout_Treatment.png')



